/**
 * @title BindPhoneActivity.java
 * @package com.esun.cheetah.view.personal
 * @description 
 * @author liugan
 * @date 2012-4-24
 * @copyright 深圳市易讯天空
 */
package com.esun.cheetah.view.personal.bindphone;

import com.esun.cheetah.R;
import com.esun.cheetah.biz.personal.bindcellphone.BindCellphoneCommunicater;
import com.esun.cheetah.biz.personal.bindcellphone.GetCodeCommunicater;
import com.esun.cheetah.biz.personal.personalinfo.UserInfo;
import com.esun.cheetah.config.Language;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

/**
 * @title BindPhoneActivity
 * @description
 * @author liugan
 */
public class BindPhoneActivity extends AbsBindPhoneActivity {

	private static final int PHONE_NUMBER_LENGTH = 11;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bind_phone_number_activity);
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
		etPhoneNumber = (EditText) findViewById(R.id.et_bind_phone_number_phone_number);
		etCode = (EditText) findViewById(R.id.et_bind_phone_number_verification_code);
		btnBind = (Button) findViewById(R.id.btn_bind_phone_number_bind_phone);
		btnSendCode = (Button) findViewById(R.id.btn_bind_phone_number_send_code);
		btnCancel = (Button) findViewById(R.id.btn_bind_phone_number_tab_back);
		btnSendCode.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				hideSoftKeys();
				final String phoneNumber = etPhoneNumber.getText().toString();
				if (phoneNumber.length() != PHONE_NUMBER_LENGTH) {
					showDialog(Language.INPUT_CORRECT_NUMBER);
				} else {
					final String ck = UserInfo.getUserInfo().getCk();
					btnSendCode.setText(Language.SENDING);
					btnSendCode.setEnabled(false);
					if (mSendCodeHandler == null) {
						mSendCodeHandler = new SendCodeHandler();
					}
					mCommunicater = new GetCodeCommunicater(mSendCodeHandler,
							null, phoneNumber, ck);
					mCommunicater.startCommunicate();
				}

			}

		});

		btnBind.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				hideSoftKeys();
				mPhoneNumber = etPhoneNumber.getText().toString();
				final String code = etCode.getText().toString();
				if (mPhoneNumber.length() != PHONE_NUMBER_LENGTH) {
					showDialog(Language.INPUT_CORRECT_NUMBER);
				} else if (TextUtils.isEmpty(code)) {
					showDialog(Language.CODE_CANNOT_BE_NULL);
				} else {
					final String ck = UserInfo.getUserInfo().getCk();
					if (mBindHandler == null) {
						mBindHandler = new BindHandler();
					}
					showProgressDialog(Language.BINDING);
					mCommunicater = new BindCellphoneCommunicater(mBindHandler,
							mPhoneNumber, code, ck);
					mCommunicater.startCommunicate();
				}
			}
		});

		btnCancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

}
